"""
.. module:: config
   :platform: Unix
   :synopsis: Parses the cernent.conf and initializes the configuration items

.. moduleauthor:: Chris White


"""

import ConfigParser, os
from deliveryMethod.Mailer import Mailer

#Configuration file location
CONFLOCATION = '/opt/cernent/etc/cernent.conf'


def _checkConfPath():
    """Checks to make sure the /opt/cernent/etc/cernent.conf file exists"""
    if not os.path.exists(CONFLOCATION):
        print("The configuration file, %s, is not present.  Please provide a configuration file at that location." % CONFLOCATION)
        exit()

        
def pullConfAndInit(init=True):
    """Uses `ConfigParser <http://docs.python.org/library/configparser.html>`_ to parse the Cernent configuration file.
    
    It places the configuration items into a dictionary called **conf** keyed by the same configuration key inserting the config as the value.

    Where it makes sense initialization and/or instantiation takes place here as well.
    
    Returns:
        dict. configuration.
    """
    _checkConfPath()
    
    config = ConfigParser.ConfigParser()
    config.read(CONFLOCATION)
    
    #Configuration dictionary
    conf = {'scanners': {}}
    
    #Loops through each section in the config file, pulling their respective configurations
    for section in config.sections():
        if 'defaults' in section:
            #Path used to store .nessus files from Nessus
            conf['archivePath'] = config.get(section, 'archivePath')
            #Path used to load report modules
            conf['reportPath'] = config.get(section, 'reportPath')
            #Path to example.nessus file for example output
            conf['examplePath'] = config.get(section, 'examplePath')
            #Comma separated list of Cernent administrator's email addresses
            conf['administrators'] = config.get(section, 'administrators')
            
        if "email" in section:
            #The default subject to use
            conf['subject'] = config.get(section, 'subject')
            
            #The default message to use, pulled from a resource file
            f = open(config.get(section, 'message'))
            conf['message'] = f.read()
            f.close()
            
        if "mailer" in section:
            #The smtp server to use when sending email.
            fromAddress = config.get(section, 'fromAddress')
            alertFromAddress = config.get(section, 'alertFromAddress')
            smtp_server = config.get(section, 'smtp_server')
            port = config.get(section, 'port') 
            username = config.get(section, 'username')
            password = config.get(section, 'password')
            
            if init:
                conf['mailer'] = Mailer(fromAddress, alertFromAddress, smtp_server, port, username, password)
            
        if "scanner" in section:
            #The configuration sections that contain scanner in the name will be put into a dictionary
            #keyed by the scanner name with a list of paramemters as the value
            conf['scanners'][config.get(section, 'name')] = [config.get(section, "hostname"), int(config.get(section, "port")),
                                                     config.get(section, "username"), config.get(section, 'password')]        
    return conf
